{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "localtext = open('14.html', 'r', encoding='utf8')\n",
    "sp = BeautifulSoup(localtext, 'lxml')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 以下是父子孙级关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6 [<p>\n",
      "<a href=\"1\">1</a>\n",
      "<a href=\"2\">2</a>\n",
      "<span>\n",
      "<p>\n",
      "<a href=\"1.5\">1.5</a>\n",
      "<a href=\"2.5\">2.5</a>\n",
      "</p>\n",
      "</span>\n",
      "</p>, <p>\n",
      "<a href=\"1.5\">1.5</a>\n",
      "<a href=\"2.5\">2.5</a>\n",
      "</p>, <p>\n",
      "<a href=\"3\">3</a>\n",
      "<a href=\"4\">4</a>\n",
      "<a href=\"5\">5</a>\n",
      "</p>, <p>\n",
      "<a href=\"6\">6</a>\n",
      "<a href=\"7\">7</a>\n",
      "</p>, <p>\n",
      "<a href=\"8\">8</a>\n",
      "<a href=\"9\">9</a>\n",
      "<a href=\"10\">10</a>\n",
      "<span>\n",
      "<p>\n",
      "<a href=\"8.5\">8.5</a>\n",
      "<a href=\"9.5\">9.5</a>\n",
      "<a href=\"10.5\">10.5</a>\n",
      "</p>\n",
      "</span>\n",
      "</p>, <p>\n",
      "<a href=\"8.5\">8.5</a>\n",
      "<a href=\"9.5\">9.5</a>\n",
      "<a href=\"10.5\">10.5</a>\n",
      "</p>]\n"
     ]
    }
   ],
   "source": [
    "## body标签下的p标签【body的子孙级】\n",
    "body_p_s = sp.select(\"body p\")\n",
    "print(len(body_p_s), body_p_s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 []\n"
     ]
    }
   ],
   "source": [
    "## body标签下的p标签【body的子级，只有一层】\n",
    "body_p__s = sp.select(\"body > p\")\n",
    "print(len(body_p__s), body_p__s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 [<p>\n",
      "<a href=\"1\">1</a>\n",
      "<a href=\"2\">2</a>\n",
      "<span>\n",
      "<p>\n",
      "<a href=\"1.5\">1.5</a>\n",
      "<a href=\"2.5\">2.5</a>\n",
      "</p>\n",
      "</span>\n",
      "</p>, <p>\n",
      "<a href=\"3\">3</a>\n",
      "<a href=\"4\">4</a>\n",
      "<a href=\"5\">5</a>\n",
      "</p>, <p>\n",
      "<a href=\"6\">6</a>\n",
      "<a href=\"7\">7</a>\n",
      "</p>, <p>\n",
      "<a href=\"8\">8</a>\n",
      "<a href=\"9\">9</a>\n",
      "<a href=\"10\">10</a>\n",
      "<span>\n",
      "<p>\n",
      "<a href=\"8.5\">8.5</a>\n",
      "<a href=\"9.5\">9.5</a>\n",
      "<a href=\"10.5\">10.5</a>\n",
      "</p>\n",
      "</span>\n",
      "</p>]\n"
     ]
    }
   ],
   "source": [
    "## div标签下的p标签【div的子级，只有一层】\n",
    "div_p__s = sp.select(\"div > p\")\n",
    "print(len(div_p__s), div_p__s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 以下是兄弟顺序关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<h1>h1</h1>]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.select('h1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6 [<h2>h2</h2>, <div>\n",
      "<p>\n",
      "<a href=\"1\">1</a>\n",
      "<a href=\"2\">2</a>\n",
      "<span>\n",
      "<p>\n",
      "<a href=\"1.5\">1.5</a>\n",
      "<a href=\"2.5\">2.5</a>\n",
      "</p>\n",
      "</span>\n",
      "</p>\n",
      "<p>\n",
      "<a href=\"3\">3</a>\n",
      "<a href=\"4\">4</a>\n",
      "<a href=\"5\">5</a>\n",
      "</p>\n",
      "</div>, <div>\n",
      "<p>\n",
      "<a href=\"6\">6</a>\n",
      "<a href=\"7\">7</a>\n",
      "</p>\n",
      "<p>\n",
      "<a href=\"8\">8</a>\n",
      "<a href=\"9\">9</a>\n",
      "<a href=\"10\">10</a>\n",
      "<span>\n",
      "<p>\n",
      "<a href=\"8.5\">8.5</a>\n",
      "<a href=\"9.5\">9.5</a>\n",
      "<a href=\"10.5\">10.5</a>\n",
      "</p>\n",
      "</span>\n",
      "</p>\n",
      "</div>, <footer>\n",
      "        底部信息1\n",
      "    </footer>, <footer>\n",
      "        底部信息2\n",
      "    </footer>, <footer>\n",
      "        底部信息3\n",
      "    </footer>]\n"
     ]
    }
   ],
   "source": [
    "## 查找h1标签往后的兄弟标签节点【范围是h1后面的全部标签】\n",
    "h1_brother_s = sp.select('h1 ~ *')\n",
    "print(len(h1_brother_s), h1_brother_s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 [<h2>h2</h2>]\n"
     ]
    }
   ],
   "source": [
    "## 查找h1标签往后的兄弟标签节点【范围是h1后面的第一个标签】\n",
    "h1_brother_s = sp.select('h1 + *')\n",
    "print(len(h1_brother_s), h1_brother_s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<div>\n",
       " <p>\n",
       " <a href=\"6\">6</a>\n",
       " <a href=\"7\">7</a>\n",
       " </p>\n",
       " <p>\n",
       " <a href=\"8\">8</a>\n",
       " <a href=\"9\">9</a>\n",
       " <a href=\"10\">10</a>\n",
       " <span>\n",
       " <p>\n",
       " <a href=\"8.5\">8.5</a>\n",
       " <a href=\"9.5\">9.5</a>\n",
       " <a href=\"10.5\">10.5</a>\n",
       " </p>\n",
       " </span>\n",
       " </p>\n",
       " </div>, <footer>\n",
       "         底部信息1\n",
       "     </footer>]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 查找body标签，子级的div标签，后面兄弟标签的第一个标签\n",
    "## 查找body标签，1个\n",
    "## 查找body子级的div，2个\n",
    "## 查找body子级div的后一个兄弟节点：\n",
    "    ## 第一个div，就得到了第二个div\n",
    "    ## 第二个div，就得到了footer底部信息1\n",
    "sp.select('body > div + *')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
